/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ServerPanels;

import ControllerSerialization.ControllerSerializer;
import GameControllerExceptions.InvalidButtonCountException;
import GameControllerExceptions.InvalidControllerException;
import Helper.ExtensionFileFilter;
import java.io.File;

import javax.swing.JFileChooser;
import javax.swing.JLayeredPane;
import javax.swing.filechooser.FileFilter;

/**
 *
 * @author luc0
 */
public class SaveOptionHandler {


    private static SaveOptionHandler instance;

    public static SaveOptionHandler getInstance(){
        if(instance == null){
            instance = new SaveOptionHandler();
        }

        return instance;
    }

    public void invokeSaveOptionFrame(JLayeredPane controllerPanel){
 //Create a file chooser
        final JFileChooser fc = new JFileChooser();
        FileFilter filter1 = new ExtensionFileFilter("XML File", new String[]{"xml"});
        fc.setFileFilter(filter1);
        fc.setApproveButtonText("Save");
        fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
        fc.setMultiSelectionEnabled(false);
        fc.setSelectedFile( new File(System.getProperty("user.home") + "/CustomController.xml"));

        fc.showSaveDialog(controllerPanel);

        File selFile = fc.getSelectedFile();

        if(selFile == null ){
            return;
        }
        if(!selFile.getAbsolutePath().contains(".xml") && !selFile.getAbsolutePath().contains(".")){
            selFile = new File(selFile.getAbsolutePath()+".xml");
        }
        ControllerSerializer serializer = ControllerSerializer.getInstance();
        try{
            serializer.savaController(controllerPanel, selFile.getAbsolutePath());
        }catch(InvalidControllerException ex){

        }catch(InvalidButtonCountException ex){

        }
    }
    private SaveOptionHandler() {
        super();
       
        

    }

}
